package jfox.platform.aaa.action;

import java.util.Collections;
import java.util.List;
import javax.ejb.EJB;

import jfox.platform.aaa.bo.RoleBO;
import jfox.platform.aaa.bo.UserBO;
import jfox.platform.aaa.entity.Role;
import jfox.platform.aaa.entity.User;
import jfox.platform.common.Utils;
import jfox.platform.infrastructure.SuperAction;
import org.jfox.framework.annotation.Service;
import org.jfox.mvc.ActionContext;
import org.jfox.mvc.Invocation;
import org.jfox.mvc.PageContext;
import org.jfox.mvc.annotation.ActionMethod;
import org.jfox.mvc.validate.DateValidation;
import org.jfox.mvc.validate.EmailValidation;
import org.jfox.mvc.validate.IntegerValidation;
import org.jfox.mvc.validate.StringDateValidator;
import org.jfox.mvc.validate.StringValidation;

/**
 * @author <a href="mailto:thaiguo@gmail.com">Taiguo Zhang</a>
 */
@Service(id = "user")
public class UserAction extends SuperAction {

    @EJB
    UserBO userBO;

    @EJB
    RoleBO roleBO;
  
    @ActionMethod(name = "mgmt", successView = "platform/aaa/user_management.vhtml")
    public void listUser(ActionContext actionContext) throws Exception {
        List<User> allUsers = userBO.getAllUsers();
        Collections.sort(allUsers);
        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("users", allUsers);
//        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$4");
    }

    
    @ActionMethod(name = "newview", successView = "platform/aaa/user_new.vhtml")
    public void newUserView(ActionContext actionContext) throws Exception {
        List<Role> roles = roleBO.getAllRoles();
//        Collections.sort(roles);
        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("roles", roles);
    }

    @ActionMethod(name = "new", successView = "include/json_response.vhtml", errorView= "include/json_response.vhtml", invocationClass = NewUserInvocation.class)
    public synchronized void newUser(ActionContext actionContext) throws Exception {
        NewUserInvocation invocation = (NewUserInvocation)actionContext.getInvocation();
        User user = new User();
        
        user.setBirthday(invocation.getBirthDay());
        
        //继承自基类
        user.setCreator(getUserContext().getUsername());
        
        user.setDepartmentId(invocation.getDepartmentId()==null?0:invocation.getDepartmentId());
        //继承自基类
        user.setDescription(invocation.getDescription()==null?"":invocation.getDescription());
        
        user.setEducationDegree(invocation.getEducationDegree()==null?0:invocation.getEducationDegree());
        user.setEmail(invocation.getEmail()==null?"":invocation.getEmail());
        //继承自基类
        user.setName(invocation.getName());
        user.setFirstName(invocation.getFirstName());
        user.setLastName(invocation.getLastName());        
        user.setPassword(invocation.getPassword());
        
        user.setGender(invocation.getGender());
        user.setGraduateSchool(invocation.getGraduateSchool()==null?"":invocation.getGraduateSchool());
        user.setGraduateYear(invocation.getGraduateYear()==null?1900:invocation.getGraduateYear());
        user.setLanguage(invocation.getLanguage()==null?0:invocation.getLanguage());
        
        //user.setLastLogin();
        
        user.setMobile(invocation.getMobile()==null?"":invocation.getMobile());
        //user.setNodes();

        user.setPhone(invocation.getPhone()==null?"":invocation.getPhone());
        //上传
        user.setPicture(invocation.getPicture()==null?"":invocation.getPicture());
        
        user.setPosition(invocation.getPosition()==null?0:invocation.getPosition());
        user.setPostAddress(invocation.getPostAddress()==null?"":invocation.getPostAddress());
        user.setPostCode(invocation.getPostCode()==null?"":invocation.getPostCode());
        //继承自基类
        user.setPriority(invocation.getPriority()==null?0:invocation.getPriority());
        
        user.setRestrictIp(invocation.getRestrictIp()==null?"":invocation.getRestrictIp());
        //继承自基类
        user.setSpeciality(invocation.getSpeciality()==null?0:invocation.getSpeciality());
        user.setTheme(invocation.getTheme()==null?0:invocation.getTheme());
        //继承自基类
        user.setVstatus(invocation.getVstatus());
        user.setLastModifier(getUserContext().getUsername());

        user.setRoles(invocation.getRolesString());

        userBO.newUser(user);
          
        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, user);
    }

 

    @ActionMethod(name = "editview", successView = "platform/aaa/user_edit.vhtml", invocationClass = GetUserInvocation.class)
    public void editUserView(ActionContext actionContext) throws Exception {
        PageContext pageContext = actionContext.getPageContext();

        GetUserInvocation invocation = (GetUserInvocation)actionContext.getInvocation();
        User user = userBO.getUserById(invocation.getId());
        pageContext.setAttribute("user", user);

        List<Role> roles = roleBO.getAllRoles();
        pageContext.setAttribute("roles", roles);
    }


    @ActionMethod(name = "edit", successView = "include/json_response.vhtml", errorView= "include/json_response.vhtml", invocationClass = EditUserInvocation.class)
    public synchronized void eidtUser(ActionContext actionContext) throws Exception {
        EditUserInvocation invocation = (EditUserInvocation)actionContext.getInvocation();
        User user = userBO.getUserById(invocation.getId());
        
        user.setBirthday(invocation.getBirthDay());

        user.setDepartmentId(invocation.getDepartmentId()==null?0:invocation.getDepartmentId());
        //继承自基类
        user.setDescription(invocation.getDescription()==null?"":invocation.getDescription());
        
        user.setEducationDegree(invocation.getEducationDegree()==null?0:invocation.getEducationDegree());
        user.setEmail(invocation.getEmail()==null?"":invocation.getEmail());
        //继承自基类
        user.setName(invocation.getName());
        user.setFirstName(invocation.getFirstName());
        user.setLastName(invocation.getLastName());        
        user.setPassword(invocation.getPassword());
        
        user.setGender(invocation.getGender());
        user.setGraduateSchool(invocation.getGraduateSchool()==null?"":invocation.getGraduateSchool());
        user.setGraduateYear(invocation.getGraduateYear()==null?1900:invocation.getGraduateYear());
        user.setLanguage(invocation.getLanguage()==null?0:invocation.getLanguage());
        
        //user.setLastLogin();
        
        user.setMobile(invocation.getMobile()==null?"":invocation.getMobile());
        //user.setNodes();

        user.setPhone(invocation.getPhone()==null?"":invocation.getPhone());
        //上传
        user.setPicture(invocation.getPicture()==null?"":invocation.getPicture());
        
        user.setPosition(invocation.getPosition()==null?0:invocation.getPosition());
        user.setPostAddress(invocation.getPostAddress()==null?"":invocation.getPostAddress());
        user.setPostCode(invocation.getPostCode()==null?"":invocation.getPostCode());
        //继承自基类
        user.setPriority(invocation.getPriority()==null?0:invocation.getPriority());
        
        user.setRestrictIp(invocation.getRestrictIp()==null?"":invocation.getRestrictIp());
        //继承自基类
        user.setSpeciality(invocation.getSpeciality()==null?0:invocation.getSpeciality());
        user.setTheme(invocation.getTheme()==null?0:invocation.getTheme());
        //继承自基类
        user.setVstatus(invocation.getVstatus());
        user.setLastModifier(getUserContext().getUsername());

        user.setRoles(invocation.getRolesString());

        userBO.updateUser(user);
            
        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, user);
    }

    
    @ActionMethod(name = "delete", successView = "include/json_response.vhtml", errorView= "include/json_response.vhtml", invocationClass = GetUserInvocation.class)
    public synchronized void deleteUser(ActionContext actionContext) throws Exception {
        
    	GetUserInvocation invocation = (GetUserInvocation)actionContext.getInvocation();
        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, invocation.getId());
        userBO.deleteUser(invocation.getId());
        
    	/*
    	try {
        	GetUserInvocation invocation = (GetUserInvocation)invocationContext.getInvocation();
            userBO.deleteUser(invocation.getId());
            JSONResponse jsonResponse = JSONResponse.createSuccessJSONResponse(null);
            invocationContext.getPageContext().setAttribute(JSON_RESPONSE, jsonResponse);
        }
        catch (Exception e) {
            JSONResponse jsonResponse = JSONResponse.createFailureJSONResponse(null, e);
            invocationContext.getPageContext().setAttribute(JSON_RESPONSE, jsonResponse);
            e.printStackTrace();
        }
        */
    }

  

    public static class NewUserInvocation extends Invocation {
        
    	@DateValidation(validatorClass = StringDateValidator.class)
    	private String birthDay;
    	
    	private Long departmentId;
    	private String description;
    	private Short educationDegree;

        @EmailValidation
        private String email;
    	private String firstName;
    	private String gender;
    	
    	private String graduateSchool;

        @IntegerValidation
        private Integer graduateYear;
    	
    	private Short language;
    	private String lastName;
    	private String mobile;
    	//登录使用的用户名
    	private String name;
    	
        private String restrictIp;

        @StringValidation(minLength = 4, maxLength = 12)
        private String password;
    	private String phone;
    	private String picture;
    	private Short position;
    	private String postAddress;
    	private String postCode;
    	private Integer priority;
    	
    	private Short speciality;
    	
    	private Short theme;
    	private int vstatus;

        private long[] roles;

        public String getFirstName() {
			return firstName;
		}

		public void setFirstName(String firstName) {
			this.firstName = firstName;
		}

		public String getLastName() {
			return lastName;
		}

		public void setLastName(String lastName) {
			this.lastName = lastName;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public String getRestrictIp() {
			return restrictIp;
		}

		public void setRestrictIp(String restrictIp) {
			this.restrictIp = restrictIp;
		}

		public String getGender() {
			return gender;
		}

		public void setGender(String gender) {
			this.gender = gender;
		}

	
		public String getEmail() {
			return email;
		}

		public void setEmail(String email) {
			this.email = email;
		}

		public String getMobile() {
			return mobile;
		}

		public void setMobile(String mobile) {
			this.mobile = mobile;
		}

		public String getPhone() {
			return phone;
		}

		public void setPhone(String phone) {
			this.phone = phone;
		}

		public String getPostAddress() {
			return postAddress;
		}

		public void setPostAddress(String postAddress) {
			this.postAddress = postAddress;
		}

		public String getPostCode() {
			return postCode;
		}

		public void setPostCode(String postCode) {
			this.postCode = postCode;
		}

		public String getPicture() {
			return picture;
		}

		public void setPicture(String picture) {
			this.picture = picture;
		}

		public String getGraduateSchool() {
			return graduateSchool;
		}

		public void setGraduateSchool(String graduateSchool) {
			this.graduateSchool = graduateSchool;
		}

		public Integer getGraduateYear() {
			return graduateYear;
		}

		public void setGraduateYear(Integer graduateYear) {
			this.graduateYear = graduateYear;
		}

		public Short getEducationDegree() {
			return educationDegree;
		}

		public void setEducationDegree(Short educationDegree) {
			this.educationDegree = educationDegree;
		}

		public Short getSpeciality() {
			return speciality;
		}

		public void setSpeciality(Short speciality) {
			this.speciality = speciality;
		}

		public Short getPosition() {
			return position;
		}

		public void setPosition(Short position) {
			this.position = position;
		}

	

		public Long getDepartmentId() {
			return departmentId;
		}

		public void setDepartmentId(Long departmentId) {
			this.departmentId = departmentId;
		}

		public Short getTheme() {
			return theme;
		}

		public void setTheme(Short theme) {
			this.theme = theme;
		}

		public Integer getPriority() {
			return priority;
		}

		public void setPriority(Integer priority) {
			this.priority = priority;
		}

		public int getVstatus() {
			return vstatus;
		}

		public void setVstatus(int vstatus) {
			this.vstatus = vstatus;
		}

		public String getDescription() {
			return description;
		}

		public void setDescription(String description) {
			this.description = description;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Short getLanguage() {
			return language;
		}

		public void setLanguage(Short language) {
			this.language = language;
		}

		public String getBirthDay() {
			return birthDay;
		}

		public void setBirthDay(String birthDay) {
			this.birthDay = birthDay;
		}

        public long[] getRoles() {
            return roles;
        }

        public void setRoles(long[] roles) {
            this.roles = roles;
        }

        public String getRolesString(){
            return Utils.array2String(getRoles());
        }
    }
    
    public static class EditUserInvocation extends NewUserInvocation {
        private long id;

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }
    }
    
    public static class GetUserInvocation extends Invocation {
        private long id;

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }
    }
    
}
